<?php
Yii::import('User.models.User');
//Yii::import('application.modules.Core.extensions.web.helpers.string',true);
class SiteController extends FrontController
{
    public function actionIndex(){
        $this->redirect(url('/index.html'));
//        $this->render('index');
    }
    
    public function getGuestAllowedActions(){
        return array('index','captcha','page','signup','login','setData');
    }
    
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
    
    /**
     * Displays the login page
     */
    public function actionLogin()
    {    
        $user = new User('login');
        if (Yii::app()->request->IsPostRequest)
        {
            if (isset($_POST['FUser']))
            {
                $_POST['User'] = $_POST['FUser'];
            }
            $result = FSM::run('User.user.login', $_POST);
            $user = $result->model;
            if ($this->post('ajax','') == 'user-form'){
                echo $result->getActiveErrorMessages($user);
                Yii::app()->end();
            }
            if (! $result->hasErrors()){
                $this->redirect(array('/userAccount/index'));
            }
        }
        if (Yii::app()->user->isGuest === false) {
            echo Yii::app()->homeUrl;
            //$this->redirect(Yii::app()->homeUrl);
        }
        
        // display the login form
        $this->render('login',array('model'=>$user));
    }
    
    public function actionSignup()
    {
        if (Yii::app()->user->isGuest === false) {
            $this->redirect(Yii::app()->homeUrl);
        }
        $user = new User();
        if (Yii::app()->request->IsPostRequest) {
            $result = FSM::run('User.user.signup', $_POST);
            if ($this->post('ajax','') == 'user-form'){
                echo $result->getActiveErrorMessages($user);
                Yii::app()->end();
            }
            if (! $result->hasErrors()) {
                $this->message = 'An email has been sent to <strong>'.CHtml::encode($result->model->email).'</strong>. Complete your registration, you need to confirm your account.';
                $this->redirect(array('login'));
            }
            else
                $user = $result->model;
        }

        $this->render('signup',array(
            'model'=>$user,
        ));
    }
    
    public function actionSetData()
    {
        Yii::import('Filly.models.FlashObject');
        $this->render('set_data', array('model' => new FlashObject()));
    }

}
